
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       sensor_airspeed_backend.c
  * @author     baiyang
  * @date       2023-8-1
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "sensor_airspeed.h"
#include "sensor_airspeed_backend.h"

#include <parameter/param.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/

/*----------------------------------variable----------------------------------*/
// access to frontend
static sensor_airspeed_t frontend = NULL;
/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void sensor_as_backend_ctor(sensor_as_backend_t as_backend, sensor_as_backend_ops_t _ops, char *name, uint8_t _instance)
{
    // 清空sensor_imu_backend结构体变量，因为sensor_imu_backend结构体有可能是申请的动态内存
    // 防止sensor_imu_backend中的指针变量初始为非零值。
    rt_memset(as_backend, 0, sizeof(struct sensor_as_backend));

    if (frontend != NULL) {
        frontend = sensor_as_get_singleton();
    }

    as_backend->ops = _ops;

    as_backend->_mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
    as_backend->instance = _instance;
}

int8_t sensor_as_backend_get_pin(sensor_as_backend_const_t as_backend)
{
    return frontend->param[as_backend->instance].pin;
}

float sensor_as_backend_get_psi_range(sensor_as_backend_const_t as_backend)
{
    return frontend->param[as_backend->instance].psi_range;
}

uint8_t sensor_as_backend_get_bus(sensor_as_backend_const_t as_backend)
{
    return frontend->param[as_backend->instance].bus;
}

bool sensor_as_backend_bus_is_confgured(sensor_as_backend_const_t as_backend)
{
    return frontend->param[as_backend->instance].bus != 1;
}

void sensor_as_backend_set_bus_id(sensor_as_backend_t as_backend, uint32_t id)
{
    param_t *param = param_get_by_variable2("ARSPD", (void*)&frontend->param[as_backend->instance].bus_id);
    if (param != NULL) {
        param_set_and_save(param, id);
    }
}

// see if voltage correction should be disabled
bool sensor_as_backend_disable_voltage_correction()
{
    return (frontend->_options & AS_OM_DISABLE_VOLTAGE_CORRECTION) != 0;
}

enum airspeed_pitot_tube_order sensor_as_backend_get_tube_order(sensor_as_backend_const_t as_backend)
{
    return (enum airspeed_pitot_tube_order)(frontend->param[as_backend->instance].tube_order);
}

float sensor_as_backend_get_airspeed_ratio(sensor_as_backend_const_t as_backend)
{
    return frontend->param[as_backend->instance].ratio;
}

// some sensors use zero offsets
void sensor_as_backend_set_use_zero_offset(sensor_as_backend_t as_backend)
{
    frontend->state[as_backend->instance].use_zero_offset = true;
}

// set to no zero cal, which makes sense for some sensors
void sensor_as_backend_set_skip_cal(sensor_as_backend_t as_backend)
{
    frontend->param[as_backend->instance].skip_cal = 1;
}

// set zero offset
void sensor_as_backend_set_offset(sensor_as_backend_t as_backend, float ofs)
{
    frontend->param[as_backend->instance].offset = ofs;
}

// set use
void sensor_as_backend_set_use(sensor_as_backend_t as_backend, int8_t use)
{
    frontend->param[as_backend->instance].use = use;
}

void as_backend_destructor(sensor_as_backend_t as_backend)
{
    if (as_backend->_mutex != NULL) {
        rt_mutex_delete(as_backend->_mutex);
        as_backend->_mutex = NULL;
    }
}
/*------------------------------------test------------------------------------*/


